package com.mw.mwreaderdemo;

import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.method.ReplacementTransformationMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class AT153 extends Activity {

	private Spinner spPwdNumber;
	private Spinner spPwdType;
	private Spinner spErea;
	private EditText etPwd;
	private EditText etData;
	private EditText etOffset;
	private EditText etLen;
	private EditText etAccess;
	private Button btnReset;
	private Button btnReadPwdErrCounter;
	private Button btnChkPwd;
	private Button btnChangePwd;
	private Button btnReadData;
	private Button btnWriteData;
	private Button btnReadAccess;
	private Button btnWriteAccess;
	private Button btnReadFuse;
	private Button btnWriteFuse;
	private EditText result;

	private int pwdNumber = 0;
	/**
	 * 密码类型 0写密码 1读密码
	 */
	private int pwdType = 0;
	/**
	 * 分区0-2应用分区 3配置分区
	 */
	private int appEare = 0;

	private int fuse = -1;

	//  上一次点击的时间 long型  
	private long beforeClick = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_at153);

		spPwdNumber = (Spinner) findViewById(R.id.spPwdNumber);
		spPwdType = (Spinner) findViewById(R.id.spPwdType);
		spErea = (Spinner) findViewById(R.id.spErea);
		etPwd = (EditText) findViewById(R.id.etPwd);
		etData = (EditText) findViewById(R.id.etData);
		etOffset = (EditText) findViewById(R.id.etOffset);
		etLen = (EditText) findViewById(R.id.etLen);
		etAccess = (EditText) findViewById(R.id.etAccess);
		btnReset = (Button) findViewById(R.id.btnReset);
		btnReadPwdErrCounter = (Button) findViewById(R.id.btnReadPwdErrCounter);
		btnChkPwd = (Button) findViewById(R.id.btnChkPwd);
		btnChangePwd = (Button) findViewById(R.id.btnChangePwd);
		btnReadData = (Button) findViewById(R.id.btnReadData);
		btnWriteData = (Button) findViewById(R.id.btnWriteData);
		btnReadFuse = (Button) findViewById(R.id.btnReadFuse);
		btnWriteFuse = (Button) findViewById(R.id.btnWriteFuse);
		btnReadAccess = (Button) findViewById(R.id.btnReadAccess);
		btnWriteAccess = (Button) findViewById(R.id.btnWriteAccess);
		result = (EditText) findViewById(R.id.result);

		// 设置输入字符转换为大写
		etPwd.setTransformationMethod(new InputLowerToUpper());
		etData.setTransformationMethod(new InputLowerToUpper());
		etAccess.setTransformationMethod(new InputLowerToUpper());

		spPwdNumber.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
				// TODO Auto-generated method stub
				pwdNumber = position;
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		spPwdType.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
				// TODO Auto-generated method stub
				pwdType = position;
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		spErea.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
				// TODO Auto-generated method stub
				appEare = position;
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		btnReset.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(AT153.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					etResultAddStr(MainActivity.reader.reset153());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnReadPwdErrCounter.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(AT153.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					int errCounter = MainActivity.reader.readPAC153(pwdNumber, pwdType);
					switch (pwdNumber) {
					case 0:
						if (pwdType == 0) {
							etResultAddStr("第一组密码写密码错误计数为: " + errCounter);
						} else {
							etResultAddStr("第一组密码读密码错误计数为: " + errCounter);
						}
						break;
					case 1:
						if (pwdType == 0) {
							etResultAddStr("第二组密码写密码错误计数为: " + errCounter);
						} else {
							etResultAddStr("第二组密码读密码错误计数为: " + errCounter);
						}
						break;
					default:
						break;
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnChkPwd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(AT153.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					MainActivity.reader.checkPwd153(pwdNumber, pwdType, etPwd.getText().toString());
					etResultAddStr("密码验证成功!");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnChangePwd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(AT153.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					MainActivity.reader.modifyPwd153(pwdNumber, pwdType, etPwd.getText().toString());
					etResultAddStr("密码修改成功!");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnReadData.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(AT153.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					int offset = Integer.parseInt(etOffset.getText().toString());
					int len = Integer.parseInt(etLen.getText().toString());
					String str = MainActivity.reader.read153(appEare, offset, len);
					Builder builder = new Builder(AT153.this);
					builder.setTitle("读出数据: ");
					builder.setMessage(str);
					builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							// TODO Auto-generated method stub

						}
					});
					builder.create();
					builder.show();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnWriteData.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(AT153.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					int offset = Integer.parseInt(etOffset.getText().toString());
					MainActivity.reader.write153(appEare, offset, etData.getText().toString());
					etResultAddStr("写数据成功!");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnReadFuse.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(AT153.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					String str = "";
					fuse = MainActivity.reader.readFuse153();
					switch (fuse) {
					case 0:
						str = "卡片已PER级熔断";
						break;
					case 4:
						str = "卡片已CMA级熔断";
						break;
					case 6:
						str = "卡片已FAB级熔断";
						break;
					case 7:
						str = "卡片没有做任何熔断";
						break;
					default:
						str = "Undefined";
						break;
					}
					etResultAddStr(str);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnWriteFuse.setOnClickListener(new OnClickListener() {

			int nextFuse = 0;

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (MainActivity.reader == null) {
					Toast.makeText(AT153.this, "设备未打开", Toast.LENGTH_SHORT).show();
					return;
				}
				String str = "";
				// 根据读出的熔断标记设置下一步的熔断标记
				switch (fuse) {
				case 0:
					str = "卡片已经达到了PER级熔断,无法继续熔断";
					Toast.makeText(AT153.this, str, Toast.LENGTH_LONG);
					return;
				case 4:
					str = "卡片将进行PER级熔断,是否继续?";
					nextFuse = 4;
					break;
				case 6:
					str = "卡片将进行CMA级熔断,是否继续?";
					nextFuse = 2;
					break;
				case 7:
					str = "卡片将进行FAB级熔断,是否继续?";
					nextFuse = 1;
					break;
				default:
					str = "卡片熔断未知,无法进行熔断,请先读取熔断标记";
					Toast.makeText(AT153.this, str, Toast.LENGTH_LONG);
					return;
				}
				Builder builder = new Builder(AT153.this);
				builder.setTitle("警告");
				builder.setMessage(str);
				builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						// TODO Auto-generated method stub
						try {
							MainActivity.reader.writeFuse153(nextFuse);
							etResultAddStr("更新熔断标记成功!");
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							etResultAddStr(e.getMessage());
						}
					}
				});
				builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						// TODO Auto-generated method stub

					}
				});
				builder.create();
				builder.show();
			}
		});

		btnReadAccess.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(AT153.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					if (appEare >= 3) {
						Toast.makeText(AT153.this, "配置区没有访问权限", Toast.LENGTH_LONG).show();
						return;
					}
					int access = MainActivity.reader.readAccess153(appEare);
					// etResultAddStr(Integer.toString(access));
					etResultAddStr("应用分区 " + appEare + " 访问权限: " + access);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnWriteAccess.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(AT153.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					int access = Integer.parseInt(etAccess.getText().toString());
					MainActivity.reader.writeAccess153(appEare, access);
					etResultAddStr("写应用分区" + access + "访问权限成功!");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		// 设置双击操作信息提示框清除数据
		result.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View arg0, MotionEvent event) {
				// TODO Auto-generated method stub
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					if (System.currentTimeMillis() - beforeClick > 500) {
						beforeClick = System.currentTimeMillis();
					} else {
						result.setText("");
					}
				}
				return false;
			}
		});
	}

	/**
	 * @Description 提示信息框添加信息
	 * @param str
	 *            要添加的信息
	 * @return void
	 */
	private void etResultAddStr(String str) {
		result.setText(result.getText() + " " + str + "\n");
		result.setSelection(result.length());// 调整光标到最后一行
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.at153, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
